﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Light.Framework
{
    /// <summary>
    /// 查询参数类：静态方法
    /// </summary>
    public partial class SearchArgs
    {
        private StringBuilder builderExt = new StringBuilder();
        private List<IDataParameter> parametersExt = new List<IDataParameter>();

        public static SearchArgs Eq(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Eq, value);
            return args;
        }
        public static SearchArgs Not(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Not, value);
            return args;
        }


        public static SearchArgs Like(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Like, value);
            return args;
        }


        public static SearchArgs Gt(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Gt, value);
            return args;
        }

        public static SearchArgs Ge(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Ge, value);
            return args;
        }

        public static SearchArgs Lt(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Lt, value);
            return args;
        }

        public static SearchArgs Le(string propertyName, object value)
        {
            SearchArgs args = new SearchArgs();
            args.Add(SearchLogic.And, propertyName, SearchOperator.Le, value);
            return args;
        }

        /// <summary>
        /// OR 逻辑运算(查询表或视图用 )
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public SearchArgs Or(SearchArgs args)
        {
            if (this.HasAnd || this.HasOr || this.builderExt.Length > 0)
                this.builderExt.Append(" OR ");
            this.builderExt.Append("(");
            this.builderExt.Append(args.GetSqlString());
            this.builderExt.Append(")");

            //追加Sql参数
            List<IDataParameter> sqlparams = args.GetDataParameters();

            foreach (var p in sqlparams)
            {
                if (!this.parametersExt.Contains(p))
                    this.parametersExt.Add(p);
            }

            return this;
        }

        /// <summary>
        /// AND 逻辑运算(查询表或视图用 )
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public SearchArgs And(SearchArgs args)
        {
            if (this.HasAnd || this.HasOr || this.builderExt.Length > 0)
                this.builderExt.Append(" AND ");
            this.builderExt.Append("(");
            this.builderExt.Append(args.GetSqlString());
            this.builderExt.Append(")");

            //追加Sql参数
            List<IDataParameter> sqlparams = args.GetDataParameters();

            foreach (var p in sqlparams)
            {
                if (!this.parametersExt.Contains(p))
                    this.parametersExt.Add(p);
            }

            return this;
        }

        /// <summary>
        /// OR 逻辑运算(查询对象用 )
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        /// <returns></returns>
        public SearchArgs Or<T>(SearchArgs args)
        {

            //追加SQL语句
            if (this.HasAnd || this.HasOr || this.builderExt.Length > 0)
                this.builderExt.Append(" OR ");
            this.builderExt.Append("(");
            string newString = args.GetSqlString<T>();

            //追加Sql参数
            List<IDataParameter> sqlparams = args.GetDataParameters<T>();

            foreach (var p in sqlparams)
            {
                string newName = p.ParameterName + "_ext_" + sqlparams.Count;
                newString = newString.Replace(p.ParameterName + " ", newName + " ");
                p.ParameterName = newName;

                this.parametersExt.Add(p);
            }
            this.builderExt.Append(newString);
            this.builderExt.Append(")");

            return this;
        }

        /// <summary>
        /// AND 逻辑运算(查询对象用 )
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        /// <returns></returns>
        public SearchArgs And<T>(SearchArgs args)
        {
            if (this.HasAnd || this.HasOr || this.builderExt.Length > 0)
                this.builderExt.Append(" AND ");
            this.builderExt.Append("(");

            string newString = args.GetSqlString<T>();

            List<IDataParameter> sqlparams = args.GetDataParameters<T>();

            foreach (var p in sqlparams)
            {
                string newName = p.ParameterName + "_ext_" + sqlparams.Count;
                newString = newString.Replace(p.ParameterName + " ", newName + " ");
                p.ParameterName = newName;

                this.parametersExt.Add(p);
            }

            this.builderExt.Append(newString);
            this.builderExt.Append(")");

            return this;
        }
    }
}
